﻿Public Class frmBroker

#Region "Properties"

    Private _CurrentBroker As Broker
    Public Property CurrentBroker() As Broker
        Get
            Return _CurrentBroker
        End Get
        Set(ByVal value As Broker)
            _CurrentBroker = value
        End Set
    End Property

    Private _bIsNew As Boolean
    Public Property IsNew() As Boolean
        Get
            Return _bIsNew
        End Get
        Set(ByVal value As Boolean)
            _bIsNew = value
        End Set
    End Property

    Private _IDToFind As Integer
    Public Property IDToFind() As Integer
        Get
            Return _IDToFind
        End Get
        Set(ByVal value As Integer)
            _IDToFind = value
        End Set
    End Property

    Private _BrokerError As ErrorHandler
    Public Property BrokerError() As ErrorHandler
        Get
            Return _BrokerError
        End Get
        Set(ByVal value As ErrorHandler)
            _BrokerError = value
        End Set
    End Property

#End Region

#Region "Clicks"

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            With Me.CurrentBroker
                .BrokerName = Me.txtBrokerName.Text
                .BrokerLocInst = Me.txtFXCCode.Text
                .BrokerBIC = Me.txtBrokerBIC.Text
                .SSI = Me.dgvSSIs.DataSource
                .IsCLS = Me.chkCLSSettle.Checked
                If .IsCLS Then
                    .CLSBIC = Me.txtCLSBIC.Text
                    .Is3rdPartySettle = Me.chkCLS3rdParty.Checked
                    .CLSAccounts = Me.dgvCLSAccounts.DataSource
                    .CLSCCYs = Me.dgvCLSCCYs.DataSource
                End If
            End With
            If Me.IsNew Then
                Me.CurrentBroker.ID = My.Settings.AllBrokers.Count
                My.Settings.AllBrokers.Add(CurrentBroker)
                Me.IsNew = False
                Me.tsslBroker.Text = Me.CurrentBroker.BrokerName & " saved."
            Else
                Me.IDToFind = CurrentBroker.ID
                Dim iBrk As Integer = My.Settings.AllBrokers.FindIndex(AddressOf FindID)
                My.Settings.AllBrokers(iBrk) = CurrentBroker
                Me.tsslBroker.Text = Me.CurrentBroker.BrokerName & " updated."
            End If
            My.Settings.Save()
        Catch ex As Exception
            Me.BrokerError.ShowMsgbox(ex, "Save Broker")
        End Try
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Me.Close()
    End Sub

    Private Sub chkCLS3rdParty_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCLS3rdParty.CheckedChanged
        ThirdPartyOnOff()
    End Sub

    Private Sub chkCLSSettle_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCLSSettle.CheckedChanged
        CLSOnOff()

    End Sub
#End Region

#Region "Private Methods"

    Private Sub CLSOnOff(Optional ByVal TurnOn As Boolean = False, Optional ByVal TurnOff As Boolean = False)

        If Me.chkCLSSettle.Checked = True OrElse TurnOn = True Then
            Me.chkCLSSettle.Checked = True
            Me.txtCLSBIC.Enabled = True
            Me.dgvCLSAccounts.Enabled = True
            Me.dgvCLSCCYs.Enabled = True
            Me.chkCLS3rdParty.Enabled = True
        ElseIf Me.chkCLSSettle.Checked = False OrElse TurnOff = True Then
            Me.chkCLSSettle.Checked = False
            Me.txtCLSBIC.Enabled = False
            Me.dgvCLSAccounts.Enabled = False
            Me.dgvCLSCCYs.Enabled = False
            Me.txt3rdPartyBIC.Enabled = False
            Me.chkCLS3rdParty.Checked = False
            Me.chkCLS3rdParty.Enabled = False
        End If
    End Sub

    Private Sub ThirdPartyOnOff(Optional ByVal TurnOn As Boolean = False, Optional ByVal TurnOff As Boolean = False)
        If Me.chkCLS3rdParty.Checked = True OrElse TurnOn = True Then
            Me.txt3rdPartyBIC.Enabled = True
        ElseIf Me.chkCLS3rdParty.Checked = False OrElse TurnOff = True Then
            Me.txt3rdPartyBIC.Enabled = False
        End If
    End Sub

    Private Sub frmBroker_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        RefreshBrokers()
    End Sub

    Private Sub RefreshBrokers()
        frmLoader.lbBrokers.Items.Clear()
        For Each brk As Broker In My.Settings.AllBrokers
            frmLoader.lbBrokers.Items.Add(brk)
        Next
    End Sub

    'Predicate Delegate for Find Method
    Private Function FindID(ByVal brk As Broker) As Boolean
        If brk.ID = IDToFind Then
            Return True
        Else
            Return False
        End If
    End Function
#End Region

#Region "Public Methods"

    Public Overloads Sub Show(ByVal EditBroker As Broker)
        Me.BrokerError = New ErrorHandler
        If EditBroker Is Nothing Then
            'new broker
            Me.IsNew = True
        Else
            Me.CurrentBroker = EditBroker
            Me.IsNew = False
        End If
        PopulateForm()
        MyBase.Show()
    End Sub

    Sub PopulateForm()
        If Me.IsNew Then
            Me.CLSOnOff(False, True)
            Me.CurrentBroker = New Broker
        Else
            With CurrentBroker
                Me.txtBrokerName.Text = .BrokerName
                Me.txtFXCCode.Text = .BrokerLocInst
                Me.txtBrokerBIC.Text = .BrokerBIC
                If .IsCLS Then
                    Me.CLSOnOff(True)
                    Me.txtCLSBIC.Text = .CLSBIC
                    Me.dgvCLSAccounts.DataSource = .CLSAccounts
                    Me.dgvCLSCCYs.DataSource = .CLSCCYs
                    Me.chkCLS3rdParty.Checked = .Is3rdPartySettle
                    If .Is3rdPartySettle Then
                        Me.ThirdPartyOnOff(True)
                        Me.txt3rdPartyBIC.Text = .ThirdPartBIC
                    End If
                Else
                    Me.CLSOnOff(False, True)
                End If
            End With
        End If

        Me.dgvCLSAccounts.DataSource = CurrentBroker.CLSAccounts
        Me.dgvCLSCCYs.DataSource = CurrentBroker.CLSCCYs
        Me.dgvSSIs.DataSource = CurrentBroker.SSI
    End Sub

#End Region




End Class